/*
	Implements the controller layer for veh
*/
package controllers

import (
	bc "github.com/ifq/veh-mng/controllers/baseController"
	"github.com/ifq/veh-mng/services/vehService"
	"github.com/ifq/tracelog"
	"github.com/ifq/veh-mng/models/vehModels"
	//"labix.org/v2/mgo/bson"
)

//** TYPES

type VehController struct {
	bc.BaseController
}

//** WEB FUNCTIONS
func (controller *VehController) InitBasic(subcate string) {
	controller.Data["category"] = "basic"
	controller.Data["subcategory"] = subcate
	controller.Layout = "shared/basic-layout.html"
	controller.TplNames = "veh/maincontent.html"
	controller.LayoutSections = map[string]string{}
	controller.LayoutSections["PageHead"] = "veh/page-head.html"
	controller.LayoutSections["Header"] = "shared/header.html"
	controller.LayoutSections["Modal"] = "shared/modal.html"
}


// Index is the initial view for the veh system
func (controller *VehController) Index() {
	controller.InitBasic("vehlist")
	
	tracelog.STARTEDf(controller.UserId, "VehController.Index", "listall")

	vehicles, err := vehService.ListVehicles(&controller.Service)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "VehController.Index", "listall")
		controller.ServeError(err)
		return
	}

	infoform := make([]vehModels.VehicleInfoForm, len(vehicles))
	for idx, veh := range vehicles {
		infoform[idx] = veh.ToFormStruct()
	}
	// 填充车辆列表
	controller.Data["Vehicles"] = infoform
	
}

func (controller *VehController) VehDetail() {
	controller.InitBasic("vehdetail")
	
	vehicles, err := vehService.ListVehicles(&controller.Service)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "VehController.Index", "detailall")
		controller.ServeError(err)
		return
	}
	infoform := make([]vehModels.VehicleInfoForm, len(vehicles))
	for idx, veh := range vehicles {
		infoform[idx] = veh.ToFormStruct()
	}
	// 填充车辆列表
	controller.Data["Vehicles"] = infoform

	// 开始根据参数找制定车辆的信息
	params := struct {
		Vehid string `form:"id"`
	}{}

	if controller.ParseAndValidate(&params) == false {
		return
	}

	vehId := params.Vehid
	tracelog.STARTEDf(controller.UserId, "VehController.VehDetail", "VehId[%s]", vehId)

	if vehId == "" {
		// xxx, 暂时这么做， 没有id 时用第一个
		vehId = infoform[0].ID
		tracelog.INFO(controller.UserId, "VehController.VehDetail", "VehId[%s]", vehId)	
		
	}
	foundveh, err := vehService.FindVehicle(&controller.Service, vehId)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "VehController.VehDetail", "VehId[%s]", vehId)
		controller.ServeError(err)
		return
	}

	detail := foundveh.ToInfoArray()

	controller.Data["Details"] = detail
}

func (controller *VehController) VehUpdateDetail() {
	controller.InitBasic("vehresult")
	params := vehModels.VehicleInfoForm{}

	if controller.ParseAndValidate(&params) == false {
		return
	}
	
	vehinfo := params.ToInfoStruct()
	
	tracelog.STARTEDf(controller.UserId, "VehController.VehDetail", "VehId[%v]", vehinfo)

	err := vehService.UpdateVehichle(&controller.Service, &vehinfo)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "VehController.VehDetail", "VehId[%v]", vehinfo)
		controller.ServeError(err)
		return
	}

	//controller.Data["backurl"] = params.Backurl
}

func (controller *VehController) DeleteVehItem() {
	controller.InitBasic("vehresult")
	params := struct {
		ID        string  `form:"objectid"`
		Backurl   string  `form:"backurl" valid:"Required"`
	}{}

	if controller.ParseAndValidate(&params) == false {
		return
	}
	
	tracelog.STARTEDf(controller.UserId, "VehController.Delete", "params[%v]", params)

	controller.Data["backurl"] = params.Backurl
}

func (controller *VehController) VehNewItemPage() {
	controller.InitBasic("vehnewpage")
	
	detail := vehModels.VehicleInfo{}
	detail.ID = "0000"
	array := detail.ToInfoArray()
	
	controller.Data["Details"] = array
}

func (controller *VehController) VehNewItem() {
	controller.InitBasic("vehresult")
	
	params := vehModels.VehicleInfoForm{}

	if controller.ParseAndValidate(&params) == false {
		return
	}
	
	vehinfo := params.ToInfoStruct()
	
	tracelog.STARTEDf(controller.UserId, "VehController.VehDetail", "VehId[%v]", vehinfo)

	err := vehService.NewVehichle(&controller.Service, &vehinfo)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "VehController.VehDetail", "VehId[%v]", vehinfo)
		controller.ServeError(err)
		return
	}
}






